import {login, register, getCode, resetpwd, jobSeekerUpdate, accountUpdate, companybasicUpdate, getResumeList, resumeUpdate, resumeAdd, deleteResumeInformSure, resumeSetSetDefault, getjobfairappliedlist} from '@/api/user';
import {getPositionList, getjobFairList, getPositionDetail, getJobfairDetail, getCompanyDetail, getJobFairAppliedList, resumePosted, jobFairAppliedIsApplied, jobFairAppliedSave, handleAddPosition, handleSureRemovePosition, handleExitPosition, handlelistForUser, handlelistForCompany} from '@/api/position';
import router from '@/router';
import axios from 'axios';

const Action = {
    // 测试
    loadArr({commit},arg){
        commit('setArr', arg);
    },

    // 登录
    async login({commit }, data) {
        const res = await login(data)
        // tslint:disable-next-line:no-shadowed-variable
        .then((res) => res.data)
        // tslint:disable-next-line:no-console
        .catch((e) => console.error(e));
        if (res.code === 0) {
            // router.app.$setCookie({"cookie":"user_token",value:res.token})
            // router.app.$setCookie({"cookie":"user_info" ,value:JSON.stringify(res.data)})

            localStorage.setItem("user_data",JSON.stringify(res.data))
            localStorage.setItem("user_token",JSON.stringify(res.token))
            axios.defaults.headers.common['token'] = res.token;
            commit('loginSuccess', res);
        } else {
            commit('snackbar', {snackbar: true, text: res.msg});
        }
        return res;
    },

    cityLocalstorToState({commit }, data) {
        commit('cityLocalstorToState', localStorage.getItem("city"))
    },

    // 注册
    async register({commit}, data){
        console.log(data)
        let res = await register(data)
        .then((res)=> res.data)
        .catch((e) => console.error(e));

        if (res.code === 0) {
            commit('unifySuccess', res);
        } else {
            commit('snackbar', {snackbar: true, text: res.msg});
        }
        return res;
    },

    // 获取职位列表
    async getPositionList({commit}, data){
        let res = await getPositionList(data)
        .then((res)=> res.data)
        .catch((e) => console.error(e));
        
        if (res.code === 0) {
            commit('getPositionListSuccess', res);
            return res;
        } else {
            commit('snackbar', {snackbar: true, text: res.msg});
        }

    },

    // 获取招聘会列表
    async getjobFairList({commit}, data){
        let res = await getjobFairList(data)
        .then((res)=> res.data)
        .catch((e) => console.error(e));
        console.log(res)
        if (res.code === 0) {
            if(res.page.rows.length > 0){
              res.page.rows.map(val=>{
                val['onlineperson'] = 0;
                val['onlinecompany'] = 0;
              })
            }
            commit('getjobFairListSuccess', res);
            return res;
        } else {
            commit('snackbar', {snackbar: true, text: res.msg});
        }

    },

    // 招聘大厅人数
    async getUserNumList({commit}, data){
        console.log(data)
        commit('getUserNumList', data);
    },

    changeSearchSelectVal({commit},arg){
        commit('changeSearchSelectVal', arg);
    },

    changeSearchVal({commit},arg){
        commit('changeSearchVal', arg);
    },

    // 初始化取用户信息
    loginLocalstorToState({commit}){
        if(
            localStorage.getItem("user_data") !== undefined 
            && localStorage.getItem("user_token") !== undefined
            && localStorage.getItem("user_data") !== null
            && localStorage.getItem("user_token") !== null
            ){
        // if(router.app.$getCookie("user_token") && router.app.$getCookie("user_info")){
            axios.defaults.headers.common['token'] = localStorage.getItem("user_token").replace(/\"/g, "");
            commit('loginLocalstorToState', {
                'token': localStorage.getItem("user_token").replace(/\"/g, ""),
                // 'globalUserInfo': router.app.$getCookie("user_info"),
                'globalUserInfo': localStorage.getItem("user_data")
            });
        }
    },

    // 退出登录
    loginOut({commit}){
        // router.app.$removeCookie("user_info");
        // router.app.$removeCookie("user_token");
        localStorage.removeItem('user_data');
        localStorage.removeItem("user_token");
        commit('loginOut');
    },

    // 获取验证码
    async getAuthCode({commit}, data){
        console.log(data)
        let res = await getCode(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)
        if (res.code === 0) {
            commit('successbar', {snackbar: true, text: res.msg});
        } else {
            commit('snackbar', {snackbar: true, text: res.msg});
        }
        return res;
    },

    // 重置验证码
    async resetpwd({commit},data){
        let res = await resetpwd(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async jobSeekerUpdate({commit},data){
        let res = await jobSeekerUpdate(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        console.log(res)
        try{
            if(res.code !== 0) throw res.msg;
            console.log(res.data)
            // router.app.$setCookie({"cookie":"user_info",value:JSON.stringify(res.data)})
            localStorage.setItem("user_data",JSON.stringify(res.data))
            
            commit('loginLocalstorToState', {
                // 'globalUserInfo': router.app.$getCookie("user_info"),
                'globalUserInfo': localStorage.getItem("user_data")
            });
            commit('successbar', {snackbar: true, text: res.msg});
         
            
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async accountUpdate({commit},data){
        let res = await accountUpdate(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        console.log(res)
        try{
            if(res.code !== 0) throw res.msg;
            // router.app.$setCookie({"cookie":"user_info",value:JSON.stringify(res.data)})
            localStorage.setItem("user_data",JSON.stringify(res.data))
            commit('loginLocalstorToState', {
                // 'globalUserInfo': router.app.$getCookie("user_info"),
                'globalUserInfo': localStorage.getItem("user_data")
            });
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    updateAvatar({commit}, res){
        console.log(res)
        try{
            if(res.code !== 0) throw res.msg;
            // router.app.$setCookie({"cookie":"user_info",value:JSON.stringify(res.data)})
            localStorage.setItem("user_data",JSON.stringify(res.data))
            commit('loginLocalstorToState', {
                // 'globalUserInfo': router.app.$getCookie("user_info"),
                'globalUserInfo': localStorage.getItem("user_data")
            });
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async companybasicUpdate({commit}, data){
        let res = await companybasicUpdate(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)

        try{
            if(res.code !== 0) throw res.msg;
            // router.app.$setCookie({"cookie":"user_info",value:JSON.stringify(res.data)})
            localStorage.setItem("user_data",JSON.stringify(res.data))
            commit('loginLocalstorToState', {
                // 'globalUserInfo': router.app.$getCookie("user_info"),
                'globalUserInfo': localStorage.getItem("user_data")
            });
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async getResumeList({commit}, data){
        let res = await getResumeList(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)

        try{
            if(res.code !== 0) throw res.msg;
            
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async resumeUpdate({commit}, data){
        let res = await resumeUpdate(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)

        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async resumeAdd({commit}, data){
        let res = await resumeAdd(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)

        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async deleteResumeInformSure({commit}, data){
        let res = await deleteResumeInformSure(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)

        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async resumeSetSetDefault({commit}, data){
        let res = await resumeSetSetDefault(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        console.log(res)

        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async getPositionDetail({commit}, data){
        let res = await getPositionDetail(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async getJobfairDetail({commit}, data){
        let res = await getJobfairDetail(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async getCompanyDetail({commit}, data){
        let res = await getCompanyDetail(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async getJobFairAppliedList({commit}, data){
        let res = await getJobFairAppliedList(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async resumePosted({commit}, data){
        let res = await resumePosted(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));

        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: '投递成功'});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async getjobfairappliedlist({commit}, data){
        let res = await getjobfairappliedlist(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async handleAddPosition({commit}, data){
        let res = await handleAddPosition(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async handleExitPosition({commit}, data){
        let res = await handleExitPosition(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async handleSureRemovePosition({commit}, data){
        let res = await handleSureRemovePosition(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        try{
            if(res.code !== 0) throw res.msg;
            commit('successbar', {snackbar: true, text: res.msg});
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async handlelistForUser({commit}, data){
        let res = await handlelistForUser(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async handlelistForCompany({commit}, data){
        let res = await handlelistForCompany(data)
        .then(res=> res.data)
        .catch((e) => console.error(e));
        try{
            if(res.code !== 0) throw res.msg;
        }catch(err){
            commit('snackbar', { 'text': err })
        }finally {
            return res;
        }
    },

    async jobFairAppliedIsApplied({commit}, data){
        console.log(data,'jobFairAppliedIsApplied-----------------------')

        if(data.apply == true){
            let res = await jobFairAppliedSave({
                jobFairId: data.jobFairId,
                comId: data.comId
            })
            .then(res=> res.data)
            .catch((e) => console.error(e));
            try{
                if(res.code !== 0) throw res.msg;
            }catch(err){
                commit('snackbar', { 'text': err })
            }finally {
                return res;
            }
        }else{
            let res = await jobFairAppliedIsApplied({
                jobFairId: data.jobFairId,
                comId: data.comId
            })
            .then(res=> res.data)
            .catch((e) => console.error(e));
            try{
                if(res.code !== 0) throw res.msg;
            }catch(err){
                commit('snackbar', { 'text': err })
            }finally {
                return res;
            }
        }

        
    },


    
}

export default Action;
